package com.oocl.statistic;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.PartialResultException;

public class Bootstrap {
	public static void main(String[] args) throws IOException {
		
		String article = statistic("article.txt");     //读取文件
		
		String numRegx = "\\d";									
 		String punRegx = "[\\pP‘’“”]";
		String engRegx = "[a-zA-Z]";
		String chRegx = "[u4e00-u9fa5]";
		
		int num = countNum(article, numRegx);        ///根据表达式得出数量
		int pun = countNum(article, punRegx);
		int eng = countNum(article, engRegx);
		int ch = countNum(article, chRegx);
		
		System.out.println("数字的数量：" + num);
		System.out.println("标点的数量：" + pun);
		System.out.println("单词的数量：" + eng);
		System.out.println("中文的数量：" + ch);
		
		
	}
	
	public static String statistic(String file) throws IOException{
		FileInputStream  in = new FileInputStream(file);
		StringBuilder sb = new StringBuilder();
		byte[] b = new byte[in.available()];
		while(in.read(b)!=-1){
			sb.append(new String(b));
		}
		return sb.toString();
		
	}
	
	
	public static int countNum(String content,String regx){
		Pattern p = Pattern.compile(regx);
		Matcher m = p.matcher(content);
		int num = 0;
		for (int i = 0; i < content.length(); i++) {
			if(m.find()){
				num++;
			}
		}
		return num;
	}
	
	
}
